'use strict';

const xtpl = require('xtpl');
const path = require('path');
const md5 = require('md5');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));


module.exports.termList = function(req,res){

    //判断是否登录
    CommonHelper.isLogin(req,res);

    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 10;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('term').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('term').alias('term').join({
            table: 'grade',
            join: 'left',
            as: 'grade',
            on: ['grade_id', 'grade_id']
        }).where(where).order('term.term_id').limit(start,limit).select().then(function(term_list){

            let data = {
                array:term_list,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"termList.html",data);
        });
    });
};

module.exports.termAdd = function(req,res){
    //判断是否登录
    CommonHelper.isLogin(req,res);

    db.table('grade').select().then(function(grade){
        db.table('term').select().then(function(term){
            let Data = {
                "grade":grade,
                "term":term
            };

            CommonHelper.renderFile(req,res,"termAdd.html",Data);
        });     
    });
};

module.exports.termAddForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    //先获取表单的数据

    let data = {
        "term_year":req.body.term_year,
        "term_start":req.body.term_start,
        "term_end":req.body.term_end,
        "grade_id":req.body.grade_id
    };

    db.table('term').add(data).then(function(insertId){
        if(insertId)
        {
            //重定向
            res.redirect(`/term/termList`);
            return false;
        }else{
            //重定向
            res.redirect(`/term/termAdd`);
            return false;
        }
    });
};

module.exports.termEdit = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let term_id = req.query.term_id;

    if(!term_id)
    {
        //重定向
        res.redirect(`/term/termList`);
        return false;
    }

    db.table('term').where(`term_id = ${term_id}`).find().then(function(term){
        db.table('grade').select().then(function(grade){
            let Data = {
                "term":term,
                "grade":grade
            };

            CommonHelper.renderFile(req,res,"termEdit.html",Data);
        });
    });
};

module.exports.termEditForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let term_id = req.query.term_id;

    if(!term_id)
    {
        //重定向
        res.redirect(`/term/termList`);
        return false;
    }

    db.table('term').where(`term_id = ${term_id}`).find().then(function(term){

        let data = {
            "term_year":req.body.term_year,
            "term_start":req.body.term_start,
            "term_end":req.body.term_end,
            "grade_id":req.body.grade_id
        };

        db.table('term').where(`term_id = ${term_id}`).update(data).then(function(affectId){
            if(affectId)
            {
                //重定向
                res.redirect(`/term/termList`);
                return false;
            }else{
                //重定向
                res.redirect(`/term/termEdit?term_id=${term_id}`);
                return false;
            }
        });
    });
};

module.exports.termDelete = function(req,res){

    let term_id = req.query.term_id;

    if(!term_id)
    {
        //重定向
        res.redirect(`/term/termList`);
        return false;
    }
    
    db.table('term').where(`term_id = ${term_id}`).delete().then(function(affectId){
        if(affectId){
            //重定向
            res.redirect(`/term/termList`);
            return false;
        }else{
            //重定向
            res.redirect(`/term/termList`);
            return false;
        }
    });
};